"use client";
import { deleteLingLog, getAllAppName, logList, resetVersion } from "@/app/actions/line_log";

import {
  createPerson,
  listPerson,
  updatePerson,
} from "@/app/actions/user_action";
import XTable from "@/components/x-table";
import type { ColumnType, SearchType } from "@/components/x-table";

import { Button, message, Modal, Space, Tag } from "antd";
import { cloneDeep } from "lodash-es";
import { useRouter, usePathname } from "next/navigation";
import { useEffect } from "react";
import utc from 'dayjs/plugin/utc'
import dayjs from "dayjs";
dayjs.extend(utc)
export default function UserPage() {
  const router = useRouter();
  const pathName = usePathname()
  const columns: ColumnType[] = [
    {
      title: "上线人",
      key: "user",
      formParams: {
        type: "select",
        mode: "multiple",
        funOption: async () => {
          return new Promise(async (resolve) => {
            const appList = await getAllAppName();
            if (appList.data) {
              let list = (appList.data as any).map((it: any) => {
                return {
                  value: it.appName,
                  label: it.appName,
                };
              });
              resolve(list as any);
            } else {
              resolve([]);
            }
          });
        },
      },
    },
    {
      title: "上线内容",
      key: "remark",
    },
    {
      title: "应用名称",
      key: "appName",
    },
    {
      title: "上线时间",
      key: "time",
      render: (text, record, index) => {
        const time = dayjs(record.time).add(8, 'hour').format('YYYY-MM-DD HH:mm:ss')
        return <Tag color="green" key={index}>{time}</Tag>
      },
    },
    {
      title: "文件大小",
      key: "fileSize",
      render: (text, record, index) => {
        let size = (record.fileSize / 1024 / 1024).toFixed(2)
        if (Number(size) < 0.5) {
          size = (record.fileSize / 1024).toFixed(2) + 'Kb'
        } else {
          size += 'M'
        }
        return <Tag color="blue" key={index}>{size}</Tag>
      },
    },
    {
      title: "上线分支",
      key: "branch",
    },
    {
      title: "操作",
      key: "action",
      render: (text, record, index) => {
        return (
          <Space>
            <Button danger type='primary'  onClick={() => {
              deleteItem(record);
            }}>删除</Button>
            {index !== 0 && record.fileName && <Button type="primary" onClick={() => {

              Modal.confirm({
                title: "确定回退到当前版本吗？",
                content: "回退到当前版本后，将覆盖线上版本",
                onOk: async () => {
                  const result = await resetVersion(record)
                  if (result.data) {
                    router.push(pathName + `?timestemp=${new Date().getTime()}`);
                    message.success("回退成功")
                  } else {
                  }
                },
              });
            }}>回退到当前</Button>}
          </Space>
        );
      },
    },
  ];
  const searchForm: SearchType[] = [
    {
      label: "上线人",
      value: "con.user",
      type: "input",
    },
    {
      label: "应用名称",
      value: "appName",
      type: "select",
      fieldKey: 'user'
    },
    {
      label: "上线时间",
      value: "time",
      type: "timerange",
    },
  ];
  const update = async (data: any) => {
    let params = cloneDeep(data);
    const res = await updatePerson(params);
    return res;
  };

  const deleteItem = (data: any) => {
    Modal.confirm({
      title: "确定删除该条数据吗？",
      content: "删除后不可恢复",
      onOk: async () => {
        const result = await deleteLingLog(data)
        if (result.data) {
          router.push(pathName + `?timestemp=${new Date().getTime()}`);
          message.success("删除成功")
        } else {
        }
      }
    });
  };
  useEffect(() => {
    getAllAppName().then((res) => {
      // console.log(res);
    });
  }, []);
  return (
    <XTable
      getList={logList}
      columns={columns}
      searchForm={searchForm}
    >
      {/* <XTable.Slot name='header'>
        </XTable.Slot> */}
    </XTable>
  );
}
